package com.api.sw.duban.util;

import weaver.conn.RecordSetTrans;

public class WhUtil {
    private int currentSequenceVal;
    private int maxSequenceVal;
    private int stepSize = 0;
    private static WhUtil instace = null;
    private WhUtil(){

    }
    public static WhUtil getInstance() throws Exception{
        if (instace == null) {
            Class var0 = WhUtil.class;
            synchronized(WhUtil.class) {
                if (instace == null) {
                    instace = new WhUtil();
                    instace.loadNextSequenceRange();
                    return instace;
                }
            }
        }

        return instace;
    }
    public int getNewRequestId() throws Exception{
        synchronized(this) {
            //if (this.currentSequenceVal == -1 || this.currentSequenceVal > this.maxSequenceVal) {
                this.loadNextSequenceRange();
            //}

            return this.currentSequenceVal;
        }
    }
    private synchronized void loadNextSequenceRange() throws Exception {
        RecordSetTrans var1 = new RecordSetTrans();
        var1.setAutoCommit(false);

        try {
            boolean var2 = var1.executeUpdate("update uf_wh set whid=whid+" + (this.stepSize + 1), new Object[0]);
            if (var2) {
                var1.executeSql("select whid from uf_wh");
                if (var1.next()) {
                    this.currentSequenceVal = var1.getInt(1);
                    //this.maxSequenceVal = this.currentSequenceVal + this.stepSize;
                    var1.commit();
                } else {
                    throw new Exception("督办ID生成失败，uf_wh，或数据库链接状态是否正常。");
                }
            } else {
                throw new Exception("督办ID生成失败，uf_wh，或ID超过列允许的最大值。");
            }
        } catch (Exception var3) {
            var1.rollback();
            throw new Exception(var3.getMessage());
        }
    }
}
